package com.android.plug;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class DexProxyAct extends Activity {

	private DexProxy dexProxy;
	private Object dexProxyObj;
	private DexClassLoader classLoader;
	private  static  final String dexPath = "/mnt/sdcard/DexFileTest-release.apk";
	private Method onCrate;;
	private Method getMainView;
	private Resources resource;
	private 	Theme mTheme  ;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//resource = createResources(this);
		File optimizedDirectoryFile = getDir("dex", 0) ;  
		Log.e("path"," path is "+optimizedDirectoryFile.getAbsolutePath());
		 classLoader = new DexClassLoader(dexPath, optimizedDirectoryFile.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());  
		   try {
			   Log.e("act"," showActivity11111");
			Class mLoadClass = classLoader.loadClass("com.example.dexfiletest.TestOperator");
			   Log.e("act"," showActivity22222");
			Constructor constructor = mLoadClass.getConstructor(Activity.class,String.class);
			   Log.e("act"," showActivity33333");
			dexProxyObj = constructor.newInstance(DexProxyAct.this,dexPath);
			   Log.e("act"," showActivity1");

			   Class cls = dexProxyObj.getClass();
		
			//  Class paramTypes[] = this.getParamTypes(cls, "showText");
			   Log.e("act"," showActivity2");
			   onCrate =cls.getDeclaredMethod("onCreate", Bundle.class);
			getMainView = cls.getDeclaredMethod("getMainView");
			   Log.e("act"," showActivity3");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch bloc
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		   try {
				Log.e("act"," showActivity");
				
				  this.setContentView((View)getMainView.invoke(dexProxyObj));
				  onCrate.invoke(dexProxyObj,savedInstanceState);
				Log.e("act"," showActivity1122");
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("act"," IllegalArgumentException");
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("act"," IllegalAccessException");
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("act"," InvocationTargetException "+e.getLocalizedMessage());
			}
		 
	}
	@Override
	public Resources getResources() {
		// TODO Auto-generated method stub
		if(resource==null){
			  AssetManager assetManager = createAssetManager(dexPath);
			  Resources superRes = super.getResources();
			  resource = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
			  Theme superTheme = super.getTheme();
				mTheme = superRes.newTheme();
		        mTheme.setTo(superTheme);
		}
		return resource;
	}
	
	@Override
	public Theme getTheme() {
		// TODO Auto-generated method stub
		if(mTheme!=null){
			return mTheme;
		}else{
			return super.getTheme();
		}
		
	}
	public static  AssetManager createAssetManager(String dexPath) {
        try {
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
            addAssetPath.invoke(assetManager, dexPath);
            return assetManager;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

   

    public static  Resources createResources(Context context,AssetManager assetManager) {
        Resources superRes = context.getResources();
        Resources resources = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
        return resources;
    }
    
    public static Resources createResources(Context context)
    {
    	AssetManager assetmager = createAssetManager(dexPath);
		Log.e("ac","2"+assetmager);
		Resources resources = createResources(context,assetmager);
		return resources;
    }
    
    public static Resources createResources(Context context,String dexPath)
    {
    	AssetManager assetmager = createAssetManager(dexPath);
		Resources resources = createResources(context,assetmager);
		return resources;
    }
}